package com.android.dbadapter;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBTeamAdapter {

	public static final String KEY_ID = "id";
	public static final String KEY_NAME = "name";
	public static final String KEY_SCORE = "score";
	private static final String TAG = "DBAdapter";

	private static final String DATABASE_NAME = "ScoreKipper.db";
	private static final String DATABASE_TABLE_TEAM = "TEAM";
	private static final int DATABASE_VERSION = 1;

	private static final String DATABASE_CREATE_TEAM = "create table TEAM (id integer primary key autoincrement, "
			+ "name varchar not null unique, score integer);";

	private final Context context;

	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;

	public DBTeamAdapter(Context ctx) {
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}

	private static class DatabaseHelper extends SQLiteOpenHelper {
		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE_TEAM);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS TEAM");
			onCreate(db);
		}
	}

	// ---opens the database---
	public DBTeamAdapter open() throws SQLException {
		db = DBHelper.getWritableDatabase();
		return this;
	}

	// ---closes the database---
	public void close() {
		DBHelper.close();
	}

	// ---insert a team into the database---
	public long insertTeam(String name) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_NAME, name);
		initialValues.put(KEY_SCORE, 0);
		return db.insert(DATABASE_TABLE_TEAM, null, initialValues);
	}

	// ---deletes a particular team---
	public boolean deleteTeam(long id) {
		return db.delete(DATABASE_TABLE_TEAM, KEY_ID + "=" + id, null) > 0;
	}

	// ---retrieves all the teams---
	public Cursor getAllTeam() {
		return db.query(DATABASE_TABLE_TEAM, new String[] { KEY_ID, KEY_NAME,
				KEY_SCORE }, null, null, null, null, null);
	}

	// ---retrieves a particular team---
	public Cursor getTeam(long id) throws SQLException {
		Cursor mCursor = db.query(true, DATABASE_TABLE_TEAM, new String[] {
				KEY_ID, KEY_NAME, KEY_SCORE }, KEY_ID + "=" + id, null, null,
				null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	// ---updates a team---
	public boolean updateTeam(long id, String name, int score) {
		ContentValues args = new ContentValues();
		args.put(KEY_NAME, name);
		args.put(KEY_SCORE, score);
		return db.update(DATABASE_TABLE_TEAM, args, KEY_ID + "=" + id, null) > 0;
	}
}
